package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.WorkConstant;
import com.zb.entity.ZjWorkFinishOrder;
import com.zb.mapper.ZjWorkFinishOrderMapper;
import com.zb.service.ZjWorkFinishOrderService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单结单表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-30
 */
@Service
public class ZjWorkFinishOrderServiceImpl extends ServiceImpl<ZjWorkFinishOrderMapper, ZjWorkFinishOrder> implements ZjWorkFinishOrderService {

    // 根据工单ID获取申请结单信息
    @Override
    public List<ZjWorkFinishOrder> getApplyEndOrderByWorkIds(List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkFinishOrder> wrapper = new QueryWrapper<>();
        wrapper.select("work_id", "gmt_create");
        wrapper.eq("status", WorkConstant.WORK_FINISH_ORDER_STATUS_1).in("work_id", workIds);
        return this.list(wrapper);
    }

    // 根据工单ID获取结单待审核的结单信息
    @Override
    public ZjWorkFinishOrder getWaitCheckStatementOrder(Long workId) {
        QueryWrapper<ZjWorkFinishOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id", workId)
                .eq("status", WorkConstant.WORK_FINISH_ORDER_STATUS_1);
        return this.getOne(wrapper);
    }

    // 根据工单ID&服务商ID获取已结单信息
    @Override
    public ZjWorkFinishOrder getEndOrderByWorkIdAndNetworkId(Long workId, Long networkId) {
        QueryWrapper<ZjWorkFinishOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id", workId)
                .in("status", WorkConstant.WORK_FINISH_ORDER_STATUS_1, WorkConstant.WORK_FINISH_ORDER_STATUS_2)
                .last("LIMIT 1");
        return this.getOne(wrapper);
    }
}
